#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\experimental\credit\basket.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Experimental { namespace Credit {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBasket
	public ref class CBasket : 
            public System::MarshalByRefObject,
            public Cephei::QL::Experimental::Credit::IBasket
	{
	protected: 
		boost::shared_ptr<QuantLib::Basket>* _ppBasket;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Basket>* _phBasket;
#endif
		Object^ _BasketOwner;     // reference to object that manages the storage for this object
	internal:
		CBasket (Cephei::IVector<String^>^ names, Cephei::IVector<Double>^ notionals, Cephei::QL::Experimental::Credit::IPool^ pool, Cephei::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ defaultKeys, Cephei::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ rrModels, Microsoft::FSharp::Core::FSharpOption<Double>^ attachmentRatio, Microsoft::FSharp::Core::FSharpOption<Double>^ detachmentRatio);
		CBasket ();
        CBasket (boost::shared_ptr<QuantLib::Basket>& childNative, Object^ owner);
        CBasket (QuantLib::Basket& childNative, Object^ owner);
        CBasket (CBasket^ copy);
        CBasket (System::Type^ t);
#ifdef STRUCT
        CBasket (QuantLib::Basket childNative);
#endif       
#ifdef HANDLE
		CBasket (QuantLib::Handle<QuantLib::Basket>& childNative, Object^ owner);
		CBasket (QuantLib::Handle<QuantLib::Basket> childNative);
#endif
		virtual ~CBasket ();
		!CBasket ();

	internal:
		QuantLib::Basket& GetReference ();
		boost::shared_ptr<QuantLib::Basket>& GetShared ();
		QuantLib::Basket* GetPointer ();
        void SetBasket (boost::shared_ptr<QuantLib::Basket> native)
        {
            if (_ppBasket != NULL)
                delete _ppBasket;
            _ppBasket = new boost::shared_ptr<QuantLib::Basket> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Basket>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property Double AttachmentAmount 
        {
		    virtual Double get () ;
        }
        property Double AttachmentRatio 
        {
		    virtual Double get () ;
        }
        property Double BasketLGD 
        {
		    virtual Double get () ;
        }
        property Double BasketNotional 
        {
		    virtual Double get () ;
        }
		virtual Double CumulatedLoss (DateTime startDate, DateTime endDate) ;
        property Double DetachmentAmount 
        {
		    virtual Double get () ;
        }
        property Double DetachmentRatio 
        {
		    virtual Double get () ;
        }
        property Cephei::IVector<Double>^ LGDs 
        {
		    virtual Cephei::IVector<Double>^ get () ;
        }
        property Cephei::IVector<String^>^ Names 
        {
		    virtual Cephei::IVector<String^>^ get () ;
        }
        property Cephei::IVector<Double>^ Notionals 
        {
		    virtual Cephei::IVector<Double>^ get () ;
        }
        property Cephei::QL::Experimental::Credit::IPool^ Pool 
        {
		    virtual Cephei::QL::Experimental::Credit::IPool^ get () ;
        }
		virtual Cephei::IVector<Double>^ Probabilities (DateTime d) ;
		virtual Double RemainingAttachmentAmount (DateTime startDate, DateTime endDate) ;
		virtual Double RemainingAttachmentRatio (DateTime startDate, DateTime endDate) ;
		virtual Double RemainingDetachmentAmount (DateTime startDate, DateTime endDate) ;
		virtual Double RemainingDetachmentRatio (DateTime startDate, DateTime endDate) ;
		virtual Cephei::IVector<String^>^ RemainingNames (DateTime startDate, DateTime endDate) ;
		virtual Double RemainingNotional (DateTime startDate, DateTime endDate) ;
		virtual Cephei::IVector<Double>^ RemainingNotionals (DateTime startDate, DateTime endDate) ;
        property Cephei::IVector<Cephei::QL::Experimental::Credit::ILoss^>^ ScenarioIncrementalBasketLosses 
        {
		    virtual Cephei::IVector<Cephei::QL::Experimental::Credit::ILoss^>^ get () ;
        }
		virtual Cephei::IVector<Cephei::QL::Experimental::Credit::ILoss^>^ ScenarioIncrementalTrancheLosses (Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ endDate) ;
		virtual Double ScenarioTrancheLoss (DateTime endDate) ;
        property UInt64 Size 
        {
		    virtual UInt64 get () ;
        }
        property Double TrancheNotional 
        {
		    virtual Double get () ;
        }
		virtual Cephei::QL::Experimental::Credit::IBasket^ UpdateScenarioLoss (Microsoft::FSharp::Core::FSharpOption<Boolean>^ zeroRecovery) ;
        property Cephei::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ DefaultKeys 
        {
		    virtual Cephei::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ get () ;
        }
        property Cephei::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ RecoveryModels 
        {
		    virtual Cephei::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ get () ;
        }
		virtual Cephei::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ RemainingDefaultKeys (DateTime startDate, DateTime endDate) ;
		virtual Cephei::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ RemainingRecModels (DateTime startDate, DateTime endDate) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CBasket_Factory : public System::MarshalByRefObject,  public IBasket_Factory
	{
	public:
        virtual IBasket^ Create (Cephei::IVector<String^>^ names, Cephei::IVector<Double>^ notionals, Cephei::QL::Experimental::Credit::IPool^ pool, Cephei::IVector<Cephei::QL::Experimental::Credit::IDefaultProbKey^>^ defaultKeys, Cephei::IVector<Cephei::QL::Experimental::Credit::IRecoveryRateModel^>^ rrModels, Microsoft::FSharp::Core::FSharpOption<Double>^ attachmentRatio, Microsoft::FSharp::Core::FSharpOption<Double>^ detachmentRatio);
        virtual IBasket^ Create ();
    };
   
/*Cephei*/ } /*QL*/ } /*Experimental*/ } /*Credit */}
